package com.threeox.ormlibrary.factory;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.alibaba.fastjson.JSONObject;
import com.threeox.ormlibrary.entity.TableMsg;
import com.threeox.ormlibrary.impl.SqlExecutor;
import com.threeox.utillibrary.util.LogUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @ClassName: DataBaseFactory
 *
 * @Description: Todo(数据库信息工厂)
 *
 * @author 赵屈犇
 *
 * @date 创建时间:2018/2/15 下午12:02
 * 
 * @version 1.0
 */
public class DataBaseFactory {

    private final String TAG = this.getClass().getName();

    private static DataBaseFactory mFactory = null;

    private Map<String, Map<String, TableMsg>> dataBaseMap = null;

    public static DataBaseFactory getInstance() {
        if (mFactory == null) {
            synchronized (DataBaseFactory.class) {
                if (mFactory == null) {
                    mFactory = new DataBaseFactory();
                }
            }
        }
        return mFactory;
    }

    private DataBaseFactory() {
        initDataBaseMap();
    }

    private void initDataBaseMap() {
        if (dataBaseMap == null) {
            this.dataBaseMap = new HashMap<String, Map<String, TableMsg>>();
        }
    }

    /**
     * 根据数据库得到表信息
     *
     * @param database
     * @param tableName
     * @return
     */
    public TableMsg getTableMessage(SQLiteDatabase database, String tableName) {
        try {
            String path = database.getPath();
            initDataBaseMap();
            Map<String, TableMsg> tableMap = null;
            if (dataBaseMap.containsKey(path)) {
                tableMap = dataBaseMap.get(path);
            } else {
                tableMap = new HashMap<String, TableMsg>();
            }
            if (tableMap.containsKey(tableName)) {
                return tableMap.get(tableName);
            } else {
                TableMsg tableMsg = queryTableMessage(database, tableName);
                tableMap.put(tableName, tableMsg);
                dataBaseMap.put(path, tableMap);
                return tableMsg;
            }
        } catch (Exception e) {
            LogUtils.e(TAG, e.getMessage());
        }
        return null;
    }

    /**
     * 查询表格信息
     *
     * @param database
     * @param tableName
     * @return
     * @throws Exception
     */
    private TableMsg queryTableMessage(SQLiteDatabase database, String tableName) throws Exception {
        SqlExecutor executor = SqlExecutor.newInstance();
        executor.setSQLiteDatabase(database);
        JSONObject data = new JSONObject();
        String sql = "select * from sqlite_master where type = 'table' AND tbl_name = ?;";
        TableMsg tableMsg = executor.query(sql, TableMsg.class, tableName);
        if (tableMsg != null) {
            Cursor column = executor.getReadableDatabase().query(tableMsg.getTbl_name(), null, null, null, null, null, "1");
            if (column != null) {
                tableMsg.setColumnNames(column.getColumnNames());
                column.close();
            }
            data.put(tableMsg.getTbl_name(), tableMsg);
        }
        return tableMsg;
    }

}
